#ifndef LUA_SCRIPT_H
#define LUA_SCRIPT_H

#include "LuaLibrary.h"
#include "Asset.h"
#include <memory>
#include <functional>

namespace Engine
{
	class LuaScript : public Asset
	{
		private:
			lua_State* m_luaState;

		public:
			LuaScript();

			//inherited from Asset
			bool Load(const std::string& fileName);
			bool Load(rapidxml::xml_node <>* node);

			void IncludeUserLibrary(LuaLibrary* l);

			bool RunScript();

			bool GetGlobal(const std::string& varName, float& f);
			bool GetGlobal(const std::string& varName, bool& b);
			bool GetGlobal(const std::string& varName, std::string& s);
			bool GetGlobal(const std::string& varName, int& i);

			void CreateNewTable(const std::string& name);

			bool GetTableField(const std::string& table, const std::string& field, float& f);
			bool GetTableField(const std::string& table, const std::string& field, bool& b);
			bool GetTableField(const std::string& table, const std::string& field, std::string& s);
			bool GetTableField(const std::string& table, const std::string& field, int& i);

			bool SetTableField(const std::string& table, const std::string& field, float f);
			bool SetTableField(const std::string& table, const std::string& field, bool b);
			bool SetTableField(const std::string& table, const std::string& field, std::string& s);
			bool SetTableField(const std::string& table, const std::string& field, int i);

			void GetLuaFunction(const std::string& name);
			bool CallLuaFunction(unsigned int numParams, unsigned int numResults);
			void PushCPPFunction(const std::string& name, lua_CFunction func);

			void Push(int v);
			void Push(float v);
			void Push(bool v);
			//must pass by ref to string not string literal
			void Push(const std::string& v);

			bool GetAndPop(int& v);
			bool GetAndPop(float& v);
			bool GetAndPop(bool& v);
			bool GetAndPop(std::string& v);

			~LuaScript();
	};

	typedef std::shared_ptr <LuaScript> LuaScriptSPtr;
}

#endif